package voloshyn.paliy.ui;

import voloshyn.paliy.arduino.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ChooseDeviceActivity extends Activity {

    // request numbers
    private static final int REQUEST_ENABLE_BT = 100;
    private String cameraMAC = null;
    // RadioGroup for listing nearby devices
    private ListView deviceList = null;
    private BluetoothAdapter bluetoothAdapter;

    private BroadcastReceiver receiver = null;

    private ArrayAdapter<String> listAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.choose_device_activity);
	initViews();
	initReceiver();
	initBluetooth();
    }

    private void initViews() {

	deviceList = (ListView) findViewById(R.id.rdgrp_deviceList);
	listAdapter = new ArrayAdapter<String>(this,
		android.R.layout.simple_list_item_1);

	deviceList.setAdapter(listAdapter);
	deviceList.setOnItemClickListener(new OnItemClickListener() {

	    @Override
	    public void onItemClick(AdapterView<?> arg0, final View arg1,
		    int arg2, long arg3) {

		AlertDialog.Builder builder = new AlertDialog.Builder(
			ChooseDeviceActivity.this);
		builder.setMessage("Do you want connect to device?")
			.setCancelable(false)
			.setPositiveButton("Yes",
				new DialogInterface.OnClickListener() {

				    @Override
				    public void onClick(DialogInterface dialog,
					    int id) {
					String deviceInfo = (String) ((TextView) arg1)
						.getText();
					String[] array = deviceInfo.split("\t");
					Intent intent = new Intent(
						getApplicationContext(),
						ControlActivity.class);
					intent.putExtra(
						getString(R.string.mac_address),
						array[array.length - 1]);
					intent.putExtra("Camera_MAC", cameraMAC);
					startActivity(intent);
				    }
				})
			.setNegativeButton("No",
				new DialogInterface.OnClickListener() {
				    @Override
				    public void onClick(DialogInterface dialog,
					    int id) {
					dialog.cancel();
				    }
				});
		AlertDialog alert = builder.create();
		alert.show();

	    }
	});
	Button buttonConnect = (Button) findViewById(R.id.btn_connect);
	buttonConnect.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		refreshList();
	    }
	});
    }

    private void initReceiver() {
	receiver = new BluetoothScanDevices(listAdapter);
    }

    private void initBluetooth() {
	bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
	if (bluetoothAdapter == null) {
	    Toast.makeText(this, getString(R.string.notSupportedBluetooth),
		    Toast.LENGTH_LONG).show();
	} else {
	    if (!bluetoothAdapter.isEnabled()) {
		Intent enableBtIntent = new Intent(
			BluetoothAdapter.ACTION_REQUEST_ENABLE);
		startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
	    }
	}
    }

    private void searchDevices() {
	if (!bluetoothAdapter.isDiscovering()) {
	    bluetoothAdapter.startDiscovery();

	}
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	if (requestCode == REQUEST_ENABLE_BT) {
	    if (resultCode == RESULT_OK) {
		searchDevices();
	    }
	} else {
	    super.onActivityResult(requestCode, resultCode, data);
	}
    }

    @Override
    protected void onPause() {
	super.onPause();
	bluetoothAdapter.cancelDiscovery();
	unregisterReceiver(receiver);
    }

    @Override
    protected void onResume() {
	super.onResume();
	registerReceiver(receiver, new IntentFilter(
		BluetoothDevice.ACTION_FOUND));
	refreshList();
    }

    private void refreshList() {
	listAdapter.clear();
	listAdapter.notifyDataSetChanged();
	bluetoothAdapter.cancelDiscovery();
	bluetoothAdapter.startDiscovery();
    }
}
